<?php
	class Advertising extends MY_controller{
		
		protected $table='advertising';
		protected $cate_table='ad_position';
		protected $redis;
		protected $redis_advertising = 'advertising';
		// function __construct(){
		// 	error_reporting(E_ALL ^ E_NOTICE); 
		// 	parent::__construct();
		// 	$this->smarty->assign("menu", 'school');
		// }

		public $left_menu_min;

		function __construct(){
			error_reporting(E_ALL ^ E_NOTICE); 
		parent::__construct();

		$this->left_menu_min = array("menu" => "网校设置", "curr" => '/school', "list" => array("学校设置" => "/school", "学校资讯" => "/news", "帮助中心" => "/help", "友情连接" => "/friend_link", "网点管理" => "/wangdian", "问答管理" => "/question_topic", "客服设置" => "/kefu"));
		  $this->smarty->assign("left_menu_min", $this->left_menu_min);
		  $this->smarty->assign("menu", 'school');

		}


		function index($sign = 0,$search = 0,$cid = 0, $limit = 0){
			$offset = 10;
			if($this->input->post()){
				$cid = $this->input->post('cid');
				$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
				$sign = $this->input->post('sign',TRUE)? urldecode($this->input->post('sign',TRUE)) : 0;
				$limit = 0;
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 5; 
			}
			$data = array('sign'=>$sign , 'search' => $search, 'cid' => $cid,  'limit' => $limit, "school_id" => $this->user_data['school_id']);
			$this->load->model('Advertisings');
			$this->load->library('pagination');
			$list = $this->Advertisings->get_list($limit, $offset, $data);
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['total_rows'] = $list['count'];
			$page_config['per_page'] = $offset;
			$page_config['base_url'] = '/advertising/index/'.$data['sign'].'/'.$data['search'].'/'.$data['cid'];
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$cate_list = $this->Advertisings->get_cat_list();
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("list", $list['result']);
			$this->smarty->assign("action", '广告列表');
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("ad/list.html");
		}


		function edit($id){
			if((int)$id > 0){
				$this->load->model('Advertisings');
				$cate_list = $this->Advertisings->get_cate_list();
				$info = get_field_by_where($this->table, '*', 'id=' . $id);
				$info['nick'] = '';
				if(($info['ad_type'] == 2 || $info['ad_type'] == 4) && !empty($info['top_id'])) {
					$user = get_field_by_where('user', 'nick', 'id=' . $info['top_id']);
					if(!empty($user['nick'])) {
						$info['nick'] = $user['nick'];
					}
				}
				$this->smarty->assign("action", '编辑广告');
				$this->smarty->assign("info", $info);
				$this->smarty->assign("cate_list", $cate_list);
			}
			$this->smarty->display("ad/save.html");
		}


		/**
		* 函数功能介绍：添加广告
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-06-11 10:04
		* 最后修改人：
		* 最后修改时间：
		*/
		function add(){
			$this->load->model('Advertisings');
			$cate_list = $this->Advertisings->get_cat_list();
			
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->assign("action", '添加广告');
			$this->smarty->display("ad/save.html");
		}


		/**
		* 函数功能介绍：保存数据
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-06-11 10:21
		* 最后修改人：
		* 最后修改时间：
		*/
		function save(){
			if( ! $this->input->post()) {
				show_message('/index', "非法操作！");exit;
			}
			$post_data = $this->input->post();
			$data = $info = array();
			$data['ad_position_sign']	= $post_data['ad_position_sign'];
			$data['ad_type']			= $post_data['ad_type'];
			$data['ad_name'] 			= $post_data['ad_name'];
			$data['ad_url'] 			= $post_data['ad_url'];
			$data['top_id']				= 0;
			$data['school_id']			= $this->user_data['school_id'];

			if($data['ad_type'] == 2) {//用户
				$data['top_id'] = !empty($post_data['uid']) ? $post_data['uid'] : 0;
				$info = get_field_by_where('user', 'id AS uid,sex,nick,head_image_url,province,city', 'id=' . $data['top_id']);
			}elseif($data['ad_type'] == 4){
				$data['top_id'] = !empty($post_data['anchor_id']) ? $post_data['anchor_id'] : 0;
				$info = get_field_by_where('user', 'id AS uid,sex,nick,head_image_url,province,city', 'id=' . $data['top_id']);
			}else if ($data['ad_type'] == 3) {//视频
				$data['top_id'] = !empty($post_data['vid']) ? $post_data['vid'] : 0;
				$info = get_field_by_where('video AS v LEFT JOIN mi_user AS u ON (v.uid = u.id)', 'v.id AS vid,v.uid,u.sex,u.nick,u.head_image_url', 'v.id=' . $data['top_id']);
			}
			
			if(!empty($post_data['ad_image_url']) && !strstr($post_data['ad_image_url'], 'http://') ){
				$data['ad_image_url'] = get_qiniu_url($post_data['ad_image_url']);
			}
			$data['sort']			= intval($post_data['sort']);
			$data['status']			= intval($post_data['status']);
			$data['add_time'] 		= !empty($post_data['add_time']) ? $post_data['add_time'] : time();
			
			if(intval($post_data['id']) > 0){
				$data['id'] = intval($post_data['id']);
				$this->update_data($data,$info);
			}else{
				$this->insert_data($data,$info);
			}
		}

		/**
		* 函数功能介绍：添加数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:31
		* 最后修改人：
		* 最后修改时间：
		*/
		function insert_data($data,$info){
			$this->db->insert($this->table, $data);
			$data['id'] = $this->db->insert_id();
			if($data['id'] > 0){
				$data = array_merge_recursive($data, $info);
				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
				$this->advertising_redis_model->redis_class->save_advertising($data['ad_position_sign'], $data);*/
				show_message($this->jump_url, "广告添加成功");
				exit;
			}else{
				show_message($this->jump_url, "广告添加失败，请重试！");
				exit;
			}
		}

		
		/**
		* 函数功能介绍：修改数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:41
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_data($data,$info = array()){
			$this->db->where('id', (int) $data['id']);
 			$result = $this->db->update($this->table, $data);
 			if($result){
				$data = array_merge_recursive($data, $info);

				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
				$this->advertising_redis_model->redis_class->save_advertising($data['ad_position_sign'], $data);*/

 				show_message($this->jump_url, "广告修改成功");
 			}else{
				show_message($this->jump_url, "广告修改失败,请重试！");
 			}
		}

		/**
		* 函数功能介绍：修改广告排序
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:46
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_sorts() {
			$this->load->model('Advertisings');
			$sort = $this->input->post('sort');
			if(is_array($sort)){
				$this->load->library('redis',array('model'=>'advertising_redis', 'channel'=>'advertising'), 'advertising_redis_model');
				foreach($sort as $key=>$val) {
					$this->Advertisings->update($key, array('sort' => $val));
					$this->advertising_redis_model->redis_class->change_sort($this->input->post('sign'), $key, $val);
				}
			}
			show_message($this->jump_url, "广告排序修改成功");
		}


		/**
		* 函数功能介绍：删除广告
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-03-05 18:04
		* 最后修改人：
		* 最后修改时间：
		* @param ids int
		*/
		function delete($id = 0){
			if($_POST){
				$this->load->model('Advertisings');
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
					foreach ($ids as $key => $value) {
						$this->advertising_redis_model->redis_class->del_ad($_POST['sign'], $value);
					}*/
					$this->Advertisings->batch_delete($ids);
					show_message($this->jump_url, "广告删除成功");
				}else{
					show_message($this->jump_url, "您没有选中任何数据");exit;
				}
			}elseif( (int) $id > 0){
				
				$this->db->where('id', $id);
				$result = $this->db->delete($this->table);
				if($this->db->affected_rows() > 0){
					/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
					$this->advertising_redis_model->redis_class->del_ad($sign, $id);*/
					show_message($this->jump_url, "广告删除成功");
				}else{
					show_message($this->jump_url, "广告删除失败，请重试！");
				}
			}
		}

		//获取二级分类
		function ignore_get_cate_by_pid($pid){
			$this->load->model('Advertisings');
			if($pid > 0){
				$result = $this->Advertisings->get_cate_id($pid);
			}
			if(is_ajax_request()){
				exit(json_encode(array("status" => 1, "result" => $result)));
			}else{
				return $result;
			}
		}
	}